package chances.wechat.repository;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.SqlSessionTemplate;

public class BaseQueryer {
	
	protected SqlSessionTemplate sqlSessionTemplate;
	
	protected SqlSessionTemplate sqlSessionTemplateMaster;

	protected String namespace = "weixin.gateway";
	
	protected String namespaceM = "weixin.gateway.master";

	protected <T> T getOne(String name, Object param) {
		String queryName = this.getName(name);
		RowBounds aRowBounds = new RowBounds(0, 1);
		List<T> result = sqlSessionTemplate.selectList(queryName, param,
				aRowBounds);
		if (result.size() == 1) {
			return result.get(0);
		} else {
			return null;
		}
	}

	protected String getName(String name) {
		return namespace + "." + name;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public String getNamespaceM(String name) {
		return namespaceM + "." + name;
	}

	public void setNamespaceM(String namespaceM) {
		this.namespaceM = namespaceM;
	}
}
